package com.have.blog.xo.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.base.constant.BlogConstant;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.holder.RequestHolder;
import com.have.blog.commons.config.jwt.Audience;
import com.have.blog.model.api.CommonPage;
import com.have.blog.model.mapper.TTodoMapper;
import com.have.blog.model.models.TTodo;
import com.have.blog.model.models.TTodoExample;
import com.have.blog.utils.MyStringUtils;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.constant.SysConstant;
import com.have.blog.xo.service.admin.AdminTodoService;
import com.have.blog.xo.vo.AdminTodoVo;
import com.have.haveblog.security.utils.JwtTokenUtil;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("all")
public class AdminTodoServiceImpl implements AdminTodoService {

    @Autowired
    private TTodoMapper todoMapper;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private Audience audience;

    @Override
    public String getPageList(AdminTodoVo todoVo) {
        PageHelper.startPage(todoVo.getCurrentPage(), todoVo.getPageSize());
        HttpServletRequest request = RequestHolder.getRequest();
        String token = request.getAttribute("token").toString();
        String adminUid = jwtTokenUtil.getAdminUid(token, audience.getBase64Secret());

        TTodoExample example = new TTodoExample();
        example.setOrderByClause("create_time desc");
        TTodoExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(todoVo.getKeyword()) && !StringUtils.isEmpty(todoVo.getKeyword().trim())) {
            criteria.andTextLike("%"+ todoVo.getKeyword() + "%");
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andAdminUidEqualTo(adminUid);
        List<TTodo> todoList = todoMapper.selectByExample(example);
        return ResultUtils.result(SysConstant.SUCCESS, CommonPage.restPage(todoList));
    }

    @Override
    public String addTodo(AdminTodoVo todoVO) {
        HttpServletRequest request = RequestHolder.getRequest();
        String token = request.getAttribute(SysConstant.TOKEN).toString();
        String adminUid = jwtTokenUtil.getAdminUid(token, audience.getBase64Secret());
        TTodo todo = new TTodo();
        todo.setText(todoVO.getText());
        todo.setUid(MyStringUtils.getUUID());
        todo.setDone(0);
        todo.setAdminUid(adminUid);
        int result = todoMapper.insert(todo);
        if (result > 0) {
            return ResultUtils.successWithMessage(MessageConstant.INSERT_FAIL);
        }
        return ResultUtils.errorWithMessage(MessageConstant.INSERT_FAIL);
    }

    @Override
    public String editTodo(AdminTodoVo todoVO) {
        TTodo todo = todoMapper.selectByPrimaryKey(todoVO.getUid());
        HttpServletRequest request = RequestHolder.getRequest();
        String token = request.getAttribute(SysConstant.TOKEN).toString();
        String adminUid = jwtTokenUtil.getAdminUid(token, audience.getBase64Secret());
        if (!todo.getAdminUid().equals(adminUid)) {
            return ResultUtils.result(SysConstant.ERROR, MessageConstant.ACCESS_NO_PRIVILEGE);
        }

        todo.setText(todoVO.getText());
        todo.setDone(todoVO.getDone()? 1:0);
        todo.setUpdateTime(new Date());
        int result = todoMapper.updateByPrimaryKeySelective(todo);
        if (result > 0) {
            return ResultUtils.successWithMessage(MessageConstant.UPDATE_SUCCESS);
        }
        return ResultUtils.errorWithMessage(MessageConstant.UPDATE_FAIL);
    }

    @Override
    public String deleteTodo(AdminTodoVo todoVO) {
        HttpServletRequest request = RequestHolder.getRequest();
        String token = request.getAttribute(SysConstant.TOKEN).toString();
        String adminUid = jwtTokenUtil.getAdminUid(token, audience.getBase64Secret());
        TTodo todo = todoMapper.selectByPrimaryKey(todoVO.getUid());

        if (!todo.getAdminUid().equals(adminUid)) {
            return ResultUtils.result(SysConstant.ERROR, MessageConstant.DATA_NO_PRIVILEGE);
        }
        todo.setStatus(BlogStatus.DISABLE.getStatus());
        todo.setUpdateTime(new Date());
        int result = todoMapper.updateByPrimaryKeySelective(todo);
        if (result > 0) {
            return ResultUtils.successWithMessage(MessageConstant.DELETE_SUCCESS);
        }
        return ResultUtils.errorWithMessage(MessageConstant.DELETE_FAIL);
    }

    @Override
    public String editBatchTodo(AdminTodoVo todoVO) {
        return null;
    }
}
